package edu.cicese.alz.swing;

/**
 * Copyright 1999 MageLang Institute
 */

import edu.cicese.alz.reports.NFCProfile;

import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;
import java.util.List;

public class CustomComboBoxModel extends AbstractListModel implements MutableComboBoxModel {

	private NFCProfile selectedProfile;

	// Define List variable
	private List<NFCProfile> profiles;

	public CustomComboBoxModel(List<NFCProfile> profiles) {
		this.profiles = profiles;
	}

	// ListModel
	public int getSize() {
		return profiles.size();
	}

	public Object getElementAt(int i) {
		// Return the element at the specified position
		return profiles.get(i);
	}

	// ComboBoxModel
	public Object getSelectedItem() {
		return selectedProfile;
//		if (selectedProfile != null) {
//			return selectedProfile;
//		}
//		else {
//			return null;
//		}
	}

	public void setSelectedItem(Object newValue) {
		selectedProfile = (NFCProfile) newValue;
	}

	// MutableComboBoxModel
	public void addElement(Object element) {
		// Insert the element
		profiles.add((NFCProfile) element);

		// Added at end, notify ListDataListener objects
		int length = getSize();
		fireIntervalAdded(this, length - 1, length - 1);
	}

	public void insertElementAt(Object element, int index) {
		// Insert the element at the specified position
		profiles.add(index, (NFCProfile) element);

		// Added in middle, notify ListDataListener objects
		fireIntervalAdded(this, index, index);
	}

	public void removeElement(Object element) {
		// Find out position
		int index = profiles.indexOf(element);

		if (index != -1) {

			// Remove an element
			profiles.remove(element);

			// Removed from middle, notify ListDataListener objects
			fireIntervalRemoved(this, index, index);
		}
	}

	public void removeElementAt(int index) {
		if (getSize() >= index) {

			// Remove an element at the specified position
			profiles.remove(index);

			// Removed from index, notify ListDataListener objects
			fireIntervalRemoved(this, index, index);
		}
	}
}